// 1厘米 = 80像素
// 1mm = 8像素
const PRINT_SCALE = 80

// 打印的大小的系数
const FONT_SIZE_SCALE = 4375

// 默认字体大小
const DEFAULT_FONT_SIZE = 24

// 是否真实打印，用于pc端调试。调试的时候可以false
const PRINT = true

/**
 * 初始化打印面板
 * @param {宽} Width
 * @param {高} Height
 */
export const initCanvas = function(Width, Height) {
	const canvasWidth = parseInt(Width * PRINT_SCALE)
	const canvasHeight = parseInt(Height * PRINT_SCALE)
	console.log("初始化面板：", canvasWidth, canvasHeight)
	PRINT && window.NativeApp.initCanvas(canvasWidth, canvasHeight);
}

/**
 * 打印静态文本
 */
export const printStaticText = function(item) {
	const Text = item['Text']
	const Left = item['Left']
	const Top = item['Top']
	const Font = item['Font']
	const Width = item['Width']
	const Height = item['Height']
	const TextAlign = item["TextAlign"]

	// 修改后的参数
	const FontSize = parseInt((Font ? Font.Size : DEFAULT_FONT_SIZE) / FONT_SIZE_SCALE)
	const FontTextAlign = getTextAlign(TextAlign)
	
	let format = JSON.stringify({
		textSize: FontSize,
		align: FontTextAlign,
		enUnderline: false,
		enBold: true,
		rotate: 0,
		width: parseInt(Width * PRINT_SCALE),
		height: parseInt(Height * PRINT_SCALE),
		xOffset: parseInt(Left * PRINT_SCALE),
		yOffset: parseInt(Top * PRINT_SCALE),
	});
	console.log("打印静态文本：", Text, "宽高：", Width, Height, "位置：", "FontTextAlign", FontTextAlign)
	PRINT && window.NativeApp.renderText(Text, format);
}

/**
 * 打印绑定文本
 * @param {Object} item
 * @param {打印数据} labelData
 */
export const printFieldText = function(item, Field, labelData) {
	const DataField = item['DataField']
	const Left = item['Left']
	const Top = item['Top']
	const Font = item['Font']
	const TextAlign = item["TextAlign"]

	const FontSize = parseInt((Font ? Font.Size : DEFAULT_FONT_SIZE) / FONT_SIZE_SCALE)
	const FontTextAlign = getTextAlign(TextAlign)

	// 绑定的字段
	const filedKey = getBindFiledByName(DataField, Field)
	if (filedKey) {
		const format = JSON.stringify({
			textSize: FontSize,
			align: FontTextAlign,
			enUnderline: false,
			enBold: true,
			rotate: 0,
			xOffset: parseInt(Left * PRINT_SCALE),
			yOffset: parseInt(Top * PRINT_SCALE),
		})
		const value = labelData[filedKey]
		if (value) {
			console.log("打印动态文本FieldBox：绑定的字段：", filedKey, "打印值：", value)
			PRINT && window.NativeApp.renderText(value, format)
		}
	}
}



/**
 * 打印综合文本
 * @param {Object} item
 * @param {打印数据} labelData
 */
export const printMemoBox = function(item, Field, labelData) {
	const DataField = item['DataField']
	const Left = item['Left']
	const Top = item['Top']
	const Font = item['Font']
	const Text = item['Text']
	const TextAlign = item["TextAlign"]

	const FontSize = parseInt((Font ? Font.Size : DEFAULT_FONT_SIZE) / FONT_SIZE_SCALE)
	const FontTextAlign = getTextAlign(TextAlign)

	// 绑定的字段
	const filedNameList = getMemoBoxBindFileds(Text)
	
	if (filedNameList && filedNameList.length > 0) {
		const format = JSON.stringify({
			textSize: FontSize,
			align: FontTextAlign,
			enUnderline: false,
			enBold: true,
			rotate: 0,
			xOffset: parseInt(Left * PRINT_SCALE),
			yOffset: parseInt(Top * PRINT_SCALE),
		})
		// 要打印的文本
		let value = Text
		filedNameList.forEach(name => {
			const filedKey = getBindFiledByName(name, Field)
			const filedValue = labelData[filedKey]
			// console.log("filedKey:", filedKey, filedValue, `[#${name}#]`)
			value = value.replace(`[#${name}#]`, filedValue)
		})
		if (value) {
			console.log("打印动态综合文本：绑定的字段：", filedNameList, "打印值：", value)
			PRINT && window.NativeApp.renderText(value, format)
		}
	}
}


/**
 * 打印条码
 * @param {Object} item
 */
export const printBarcode = function(item, Field, labelData) {
	const Text = item['Text']
	const Left = item['Left']
	const Top = item['Top']
	const Width = item['Width']
	const Height = item['Height']

	const Name = item['Name']

	const format = JSON.stringify({
		dotWidth: 1, //必要
		barHeight: 80, //必要
		readable: 0,
		align: 0,
		rotate: 0,
		width: parseInt(Width * PRINT_SCALE),
		height: parseInt(Height * PRINT_SCALE),
		xOffset: parseInt(Left * PRINT_SCALE),
		yOffset: parseInt(Top * PRINT_SCALE)
	});

	const QrCodeText = getBarcodeText(item["Text"])
	if (QrCodeText) {
		const filedKey = getBindFiledByName(QrCodeText, Field)
		const value = labelData[filedKey]
		if (value !== '' && value) {
			console.log("打印条码：", "绑定字段:", filedKey, "值：", value)
			PRINT && window.NativeApp.renderBarCode("123456", format);
		}
	}
}


/**
 * 打印二维码
 * @param {Object} item
 */
export const printQrCode = function(item, Field, labelData) {
	const Left = item['Left']
	const Top = item['Top']
	const Width = item['Width']
	const Height = item['Height']

	let format = JSON.stringify({
		dotSize: 4,
		rotate: 0,
		width: parseInt(Width * PRINT_SCALE),
		height: parseInt(Height * PRINT_SCALE),
		xOffset: parseInt(Left * PRINT_SCALE),
		yOffset: parseInt(Top * PRINT_SCALE),
	});
	const QrCodeText = getBarcodeText(item["Text"])
	if (QrCodeText) {
		const filedKey = getBindFiledByName(QrCodeText, Field)
		console.log("二维码绑定字段：", filedKey)
		const value = labelData[filedKey]
		if (value !== '' && value) {
			console.log("打印二维码：", value)
			PRINT && window.NativeApp.renderQrCode(labelData[filedKey], format);
		}
	}
}



/**
 * 打印线条
 * @param {Object} item
 */
export const printLine = function(item) {
	const Left = item['Left']
	const Top = item['Top']
	const Height = item['Height']
	const Width = item['Width']

	// 线条坐标
	const xOffset = parseInt(Left * PRINT_SCALE)
	const yOffset = parseInt(Top * PRINT_SCALE)
	const endX = Width ? parseInt(Width * PRINT_SCALE) : xOffset
	const endY = Height ? parseInt(Height * PRINT_SCALE) : yOffset
	
	const format = JSON.stringify({
		style: 4,
		thickness: 2,
		xOffset,
		yOffset,
		endX,
		endY,
	});
	console.log("打印线条：", format)
	PRINT && window.NativeApp.renderArea(format);
}


/**
 * 打印
 * @param {打印份数} number
 */
export const printCanvas = function(number) {
	PRINT && window.NativeApp.printCanvas(number)
}

/**
 * 当字段是条码的时候，获取filed
 * @param {Object} text = " [#条码#] "
 */
function getBarcodeText(text) {
	if (!text) return null
	var startIndex = text.indexOf("[#");
	var endIndex = text.indexOf("#]");

	if (startIndex !== -1 && endIndex !== -1) {
		return text.substring(startIndex + 2, endIndex);
	} else {
		return null;
	}
}


/**
 * 获取综合文本的 filed
 * @param {Object} text = " [#产品编号#]-[#色号#][#产品名称#] "
 * @return ["产品编号", "色号", "产品名称"]
 */
function getMemoBoxBindFileds(text) {
	return extractStr(text)
}

/**
 * 获取 [#产品编号#]-[#色号#][#产品名称#] 中的中文
 * @param {Object} str
 */
function extractStr(str) {
  var re = /#(.*?)#/g; // 匹配#之间的所有字符
  var result = [];
  var match;
  while (match = re.exec(str)) {
    result.push(match[1]); // 将匹配到的内容存入数组中
  }
  return result;
}

/**
 * 根据中文名称，获取真实的变量
 * 比如：条码 -> sn
 */
function getBindFiledByName(name, Field = []) {
	const targetFiled = Field.find(item => {
		return item.Name === name
	})
	if (targetFiled) {
		return targetFiled.DBFieldName
	}
}

/**
 * 0,1,2: 左中右
 * @param {Object} TextAlign 
 */
function getTextAlign(TextAlign) {
	const maps = {
		"MiddleCenter": 1,
		"MiddleRight": 2
	}
	return maps[TextAlign] || 0
}